<template>
	<view class="personal-center-page">
		<!-- 沉浸式状态栏占位 -->
		<view class="status-bar" :style="{ height: statusBarHeight + 'px' }"></view>
		
		<!-- 顶部背景图区域 -->
		<view class="top-background-section">
			<view class="page-title">个人中心</view>
			
			<!-- 用户信息卡片 -->
			<view class="user-card" @tap="editProfile">
				<view class="user-info">
					<view class="avatar-container">
						<image class="user-avatar" src="/static/my/touxiang.png" mode="aspectFill"></image>
						<view class="edit-icon">
							<image src="/static/my/Union@2x.png" class="edit-image" mode="aspectFit"></image>
						</view>
					</view>
					<view class="user-details">
						<view class="user-header">
							<text class="user-name">{{ userInfo.name }}</text>
							<view class="department-tag">
								<text class="department-text">{{ userInfo.department }}</text>
							</view>
						</view>
						<text class="user-phone">{{ userInfo.phone }}</text>
					</view>
				</view>
				<view class="arrow-icon">
					<image src="/static/my/Vector1@2x.png" style="width: 32rpx; height: 32rpx;" class="arrow" mode="aspectFit"></image>
					<!-- <text class="arrow">></text> -->
				</view>
			</view>
		</view>

		<!-- 通讯录 -->
		<view class="contacts-section">
			<view class="section-header">
				<text class="section-title">通讯录</text>
				<text class="contacts-count" v-if="!loading">({{ contactsList.length }})</text>
			</view>
			
			<!-- 加载状态 -->
			<view class="loading-container" v-if="loading">
				<text class="loading-text">加载中...</text>
			</view>
			
			<!-- 空状态 -->
			<view class="empty-container" v-else-if="contactsList.length === 0">
				<text class="empty-text">暂无通讯录</text>
			</view>
			
			<!-- 联系人列表 -->
			<view class="contacts-list" v-else>
				<view class="contact-item" v-for="(contact, index) in contactsList" :key="contact.id">
					<view class="contact-info">
						<image class="contact-avatar" :src="contact.avatar" mode="aspectFill"></image>
						<view class="contact-details">
							<text class="contact-name">{{ contact.name }}</text>
							<text class="contact-department">{{ contact.department }}</text>
						</view>
					</view>
					<view class="call-btn" @tap="makeCall(contact)">
						<image src="/static/gongzt/Frame 1799@2x.png" class="call-icon" mode="aspectFit"></image>
					</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
import { apiGetUserList } from '@/request/api.js'

export default {
	data() {
		return {
			statusBarHeight: 0,
			loading: false,
			userInfo: {
				name: '加载中...',
				department: '养老部',
				phone: '',
				avatar: '/static/my/touxiang.png'
			},
			contactsList: [],
			// 当前用户ID - 实际应从登录信息或本地存储获取
			currentUserId: 1
		}
	},
	onLoad() {
		// 获取系统信息
		const systemInfo = uni.getSystemInfoSync();
		this.statusBarHeight = systemInfo.statusBarHeight || 0;
		
		// 加载用户数据
		this.loadUserData();
	},
	
	// 页面显示时刷新数据
	onShow() {
		this.loadUserData();
	},
	methods: {
		// 加载用户数据
		async loadUserData() {
			if (this.loading) return;
			
			this.loading = true;
			
			try {
				// 构建请求参数
				const data = {
					uid: this.currentUserId
				};
				
				const response = await apiGetUserList(data);
				
				if (response.code === 0 && response.data) {
					const apiData = response.data;
					
					// 设置当前用户信息（从info字段获取）
					if (apiData.info) {
						this.setCurrentUserInfo(apiData.info);
					}
					
					// 设置通讯录列表（从data数组获取，排除当前用户）
					if (apiData.data && Array.isArray(apiData.data)) {
						this.setContactsList(apiData.data);
					}
				} else {
					uni.showToast({
						title: response.msg || '获取用户数据失败',
						icon: 'none'
					});
				}
			} catch (error) {
				console.error('加载用户数据失败:', error);
				uni.showToast({
					title: '网络错误，请重试',
					icon: 'none'
				});
			} finally {
				this.loading = false;
			}
		},
		
		// 设置当前用户信息
		setCurrentUserInfo(userInfo) {
			this.userInfo = {
				name: userInfo.name || '未知用户',
				department: this.getDepartmentName(userInfo.part, userInfo.position),
				phone: this.formatPhone(userInfo.phone),
				avatar: userInfo.photo || '/static/my/touxiang.png'
			};
		},
		
		// 设置通讯录列表
		setContactsList(userList) {
			// 过滤掉当前用户，只显示其他用户
			const contacts = userList
				.filter(user => user.id !== this.currentUserId)
				.map(user => ({
					id: user.id,
					name: user.name || '未知用户',
					department: this.getDepartmentName(user.part, user.position),
					avatar: user.photo || this.getDefaultAvatar(),
					phone: user.phone || user.contact_phone || ''
				}))
				.filter(contact => contact.phone); // 只显示有电话的联系人
			
			this.contactsList = contacts;
		},
		
		// 获取部门名称
		getDepartmentName(part, position) {
			if (part && position) {
				return `${part}·${position}`;
			} else if (part) {
				return part;
			} else if (position) {
				return position;
			} else {
				return '养老部·护工';
			}
		},
		
		// 格式化电话号码
		formatPhone(phone) {
			if (!phone) return '';
			
			// 隐藏手机号中间4位
			if (phone.length === 11) {
				return phone.substring(0, 3) + '****' + phone.substring(7);
			}
			return phone;
		},
		
		// 获取默认头像
		getDefaultAvatar() {
			return '';
		},
		
		// 编辑个人资料
		editProfile() {
			uni.navigateTo({
				url: `/pages/userInfo/userInfo?userId=${this.currentUserId}`
			});
		},

		// 拨打电话
		makeCall(contact) {
			if (!contact.phone) {
				uni.showToast({
					title: '该联系人没有电话号码',
					icon: 'none'
				});
				return;
			}
			
			uni.showModal({
				title: '拨打电话',
				content: `确定要拨打 ${contact.name} 的电话吗？`,
				success: (res) => {
					if (res.confirm) {
						// 拨打电话
						uni.makePhoneCall({
							phoneNumber: contact.phone,
							success: () => {
								console.log('拨打电话成功');
							},
							fail: (err) => {
								console.log('拨打电话失败', err);
								uni.showToast({
									title: '拨打电话失败',
									icon: 'none'
								});
							}
						});
					}
				}
			});
		}
	}
}
</script>

<style lang="scss" scoped>
.personal-center-page {
	height: 94vh;
	background: #F8F8F8;
	display: flex;
	flex-direction: column;
	overflow: hidden;
}

.status-bar {
	background-image: url('/static/my/top_bj@2x.png');
	background-size: cover;
	background-position: center top;
	background-repeat: no-repeat;
}

.top-background-section {
	background-image: url('/static/my/top_bj@2x.png');
	background-size: cover;
	background-position: center;
	background-repeat: no-repeat;
	padding: 30rpx 30rpx 40rpx;
	color: #333333;
}

.page-title {
	font-size: 36rpx;
	font-weight: bold;
	color: #333333;
	margin-bottom: 40rpx;
}

.user-card {
	border-radius: 16rpx;
	padding: 30rpx;
	border: 1rpx solid rgba(255, 255, 255, 0.5);
	backdrop-filter: blur(10rpx);
	display: flex;
	align-items: center;
	justify-content: space-between;
}

.user-info {
	display: flex;
	align-items: center;
	flex: 1;
}

.avatar-container {
	position: relative;
	margin-right: 24rpx;
}

.user-avatar {
	width: 120rpx;
	height: 120rpx;
	border-radius: 60rpx;
	background-color: #F0F0F0;
}

.edit-icon {
	position: absolute;
	bottom: 0;
	right: 0;
	width: 36rpx;
	height: 36rpx;
	background: #FF4856;
	border-radius: 50%;
	border: 4rpx solid #FFFFFF;
	display: flex;
	align-items: center;
	justify-content: center;
}

.edit-image {
	width: 20rpx;
	height: 20rpx;
	filter: brightness(0) invert(1);
}

.user-details {
	flex: 1;
}

.user-header {
	display: flex;
	align-items: center;
	margin-bottom: 12rpx;
}

.user-name {
	font-size: 36rpx;
	font-weight: bold;
	color: #333333;
	margin-right: 16rpx;
}

.department-tag {
	border-radius: 8rpx;
	padding: 6rpx 12rpx;
}

.department-text {
	font-size: 22rpx;
	color: #FF6F1C;
	font-weight: 500;
}

.user-phone {
	font-size: 28rpx;
	color: #666666;
}

.arrow-icon {
	margin-left: 20rpx;
}

.arrow {
	font-size: 32rpx;
	color: #CCCCCC;
}

.contacts-section {
	background: #FFFFFF;
	margin: 20rpx;
	border-radius: 16rpx;
	padding: 30rpx;
	box-shadow: 0 2rpx 8rpx rgba(0, 0, 0, 0.05);
	flex: 1;
	display: flex;
	flex-direction: column;
	overflow: hidden;
}

.section-header {
	margin-bottom: 20rpx;
	flex-shrink: 0;
}

.section-title {
	font-size: 32rpx;
	font-weight: bold;
	color: #333333;
}

.contacts-count {
	font-size: 24rpx;
	color: #999999;
	margin-left: 8rpx;
}

.contacts-list {
	display: flex;
	flex-direction: column;
	flex: 1;
	overflow-y: auto;
}

.contact-item {
	display: flex;
	align-items: center;
	justify-content: space-between;
	padding: 20rpx 0;
	border-bottom: 1rpx solid #F0F0F0;
}

.contact-item:last-child {
	border-bottom: none;
}

.contact-info {
	display: flex;
	align-items: center;
	flex: 1;
}

.contact-avatar {
	width: 80rpx;
	height: 80rpx;
	border-radius: 40rpx;
	background-color: #F0F0F0;
	margin-right: 20rpx;
}

.contact-details {
	flex: 1;
}

.contact-name {
	font-size: 30rpx;
	font-weight: 500;
	color: #333333;
	display: block;
	margin-bottom: 8rpx;
}

.contact-department {
	font-size: 26rpx;
	color: #666666;
	display: block;
}

.call-btn {
	width: 88rpx;
	height: 60rpx;
	background: linear-gradient(135deg, #FF4856 0%, #FF6F1C 100%);
	border-radius: 30rpx;
	display: flex;
	align-items: center;
	justify-content: center;
	box-shadow: 0 4rpx 12rpx rgba(255, 72, 86, 0.3);
}

.call-btn:active {
	transform: scale(0.95);
	box-shadow: 0 2rpx 6rpx rgba(255, 72, 86, 0.3);
}

.call-icon {
	width: 32rpx;
	height: 32rpx;
	filter: brightness(0) invert(1);
}

/* 加载状态和空状态样式 */
.loading-container,
.empty-container {
	display: flex;
	justify-content: center;
	align-items: center;
	padding: 80rpx 0;
	flex: 1;
}

.loading-text,
.empty-text {
	font-size: 28rpx;
	color: #999999;
}
</style> 